package com.yxq.mdt.app.modules.personal.adapter;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.bottomsheet.CommentBottomSheetDialog;
import com.yxq.mdt.app.modules.personal.bean.MainPersonalStateBean;
import com.yxq.mdt.app.modules.picturebrowse.ui.PictureActivity;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialPersonalHomepageAc;
import com.yxq.mdt.app.modules.socialcontact.ui.fragment.SocialStateCommentFragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author L
 * @time
 * @describe 我的回复
 */
public class CommentReplyListAdapter extends BaseQuickAdapter<MainPersonalStateBean.ListBean, BaseViewHolder> {
    CommentBottomSheetDialog commentBottomSheetDialog;
    FragmentManager comment;
    MyReplyCommentListAdapter adapter;
    SocialStateCommentFragment socialStateCommentFragment;

    public CommentReplyListAdapter(int layoutResId, List<MainPersonalStateBean.ListBean> data, FragmentManager fragmentManager, SocialStateCommentFragment socialStateCommentFragment) {
        super(layoutResId, data);
        comment = fragmentManager;
        this.socialStateCommentFragment = socialStateCommentFragment;
    }

    @Override
    protected void convert(BaseViewHolder helper, MainPersonalStateBean.ListBean item) {

        GlideHelper.setLoadRoundImage(mContext, item.getHeadimg(), 0, helper.getView(R.id.personal_item_img));
        helper.setText(R.id.item_text, item.getContent());
        if (!TextUtils.isEmpty(item.getUserName())) {
            helper.setText(R.id.item_layout_nike_name, item.getUserName());
        } else {
            helper.setText(R.id.item_layout_nike_name, "yxq" + item.getUserId());
        }
        if (item.getSex() == 1) {
            helper.setImageResource(R.id.item_layout_nike_sex, R.mipmap.xb_nan);
        } else {
            helper.setImageResource(R.id.item_layout_nike_sex, R.mipmap.xb_nv);
        }
        helper.setText(R.id.item_layout_time, TimeUtils.getFriendlyTimeSpanByNow(item.getCreateDate(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
        if (item.getGiveLike() != 0) {
            helper.setText(R.id.give_num, item.getGiveNum() + "");
            helper.setImageResource(R.id.item_layout_attention, R.mipmap.zan);
        } else {
            helper.setText(R.id.give_num, "赞");
            helper.setImageResource(R.id.item_layout_attention, R.mipmap.icon_parise_normal);
        }
        helper.setOnClickListener(R.id.item_layout_reply, view -> {
            commentBottomSheetDialog = new CommentBottomSheetDialog(socialStateCommentFragment, item.getTopicId(), adapter.getItem(0).getReplyId(), 1);
            commentBottomSheetDialog.show(comment, "commentBottomSheetDialog");
        });
        helper.addOnClickListener(R.id.item_layout_reply);
        helper.addOnClickListener(R.id.give_like_lint);

        //回复的图片
        RecyclerView recyclerViewPhoto = helper.getView(R.id.item_layout_attention_img);
        if (item.getFileVoList() != null && item.getFileVoList().size() > 0) {
            //有评论图片
            recyclerViewPhoto.setVisibility(View.VISIBLE);
            CommentReplyListImageAdapter commentReplyListImageAdapter = new CommentReplyListImageAdapter(R.layout.item_comment_img, item.getFileVoList());
            recyclerViewPhoto.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
            recyclerViewPhoto.setAdapter(commentReplyListImageAdapter);
            commentReplyListImageAdapter.setOnItemClickListener((adapter, view, position) -> {
                Intent intent = new Intent(mContext, PictureActivity.class);
                intent.putExtra("position", position);
                ArrayList<String> images = new ArrayList<>();
                for (MainPersonalStateBean.ListBean.FileVoListBean fileListBean : item.getFileVoList()) {
                    images.add(fileListBean.getFile());
                }
                intent.putExtra("images", images);
                mContext.startActivity(intent);
            });

        } else {
            recyclerViewPhoto.setVisibility(View.GONE);
        }
        //回复的内容
        RecyclerView recyclerView = helper.getView(R.id.recyclerView);
        helper.addOnClickListener(R.id.item_reply);
        if (item.getChiledrenList() != null && item.getChiledrenList().size() > 0) {
            recyclerView.setVisibility(View.VISIBLE);
            //有回复数据
            MyReplyCommentListAdapter adapter = new MyReplyCommentListAdapter(R.layout.item_reply_publish_comment_two_layout, item.getChiledrenList());
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            recyclerView.setAdapter(adapter);
            adapter.setOnItemChildClickListener((adapter1, view, position) -> {
                switch (view.getId()) {
                    case R.id.item_reply:
                        commentBottomSheetDialog = new CommentBottomSheetDialog(socialStateCommentFragment, item.getTopicId(), adapter.getItem(0).getReplyId(), 1);
                        commentBottomSheetDialog.show(comment, "commentBottomSheetDialog");
                        break;
                    case R.id.item_layout_nike_name:
                        Intent it = new Intent(mContext, SocialPersonalHomepageAc.class);
                        it.putExtra("userId", adapter.getItem(position).getUserId());
                        mContext.startActivity(it);
                        break;
                }
            });
        } else {
            recyclerView.setVisibility(View.GONE);
        }
    }
}
